module AniadirNuevoCorteWindow (
    aniadirNuevoCorteWindowAcid
    ) where

{-
AUTOR:Gaston Charkiewicz
DESCRIPCION: La ventana de aniadir un nuevo corte a realizarse, a un usuario en particular.
OBSERVACIONES:
-}

import Graphics.UI.Gtk
import MainHandler
import Customer
import Data.IORef
import Haircut
import WindowUtils
import PerformedHaircut

 
aniadirNuevoCorteWindowAcid customer state  = do
  initGUI
  window <- windowNew
  window `onDestroy` mainQuit
  windowSetPosition window WinPosCenter
  
  -- Label de fantasia para que no quede pegado arriba
  fantasiaLabel <- labelNew (Just "")
  
  -- Para empaquetar, una principal y una auxiliar
  vbox <- vBoxNew False 10
  hbox <- hBoxNew True 4
  
  -- El combo
  combo <- comboHaircutsAcid state
  
  -- El label Observaciones
  lab <- labelNew (Just "Observaciones:")
  
  -- El textField para las observaciones
  txtfield <- entryNew
  buttonAceptar <- buttonNewWithLabel "Aceptar"
  
  -- Crear uno nuevo 
  onClicked buttonAceptar (addPerfomedHaircutToCustomerAcid customer (getSelectedHaircutAcid combo state) (entryGetText txtfield) state window)
  
  -- Boton Volver atras
  buttonVolver <- buttonNewWithLabel "Volver"
  onClicked buttonVolver (widgetDestroy window)
  
  -- Seteo tipificaciones de la ventana
  set window [windowTitle := "Añadir corte a Cliente",windowDefaultWidth := 300,
        windowDefaultHeight := 250, containerBorderWidth := 10, containerChild := vbox]
  
  -- Empaquetado de la hBox auxiliar
  boxPackStart hbox buttonAceptar PackGrow 1  
  boxPackStart hbox buttonVolver PackGrow 1 
  
  -- Empaquetado principal
  boxPackStart vbox fantasiaLabel PackNatural 1
  boxPackStart vbox combo PackNatural 1
  boxPackStart vbox lab PackNatural 1
  boxPackStart vbox txtfield PackNatural 1
  boxPackEnd vbox hbox PackNatural 1
  
  -- Finalmente, muestro todo
  widgetShowAll window
  mainGUI
 
---------------------------------------------------------------------------
--------------------- CONTROLLERS CON ACID -----------------------------------
  
comboHaircutsAcid state = do
 
  haircuts <- getHaircutsAcid state
  store <- listStoreNew (haircuts)
  combo <- comboBoxEntryNewWithModel store
  customStoreSetColumn store textColumn (getHaircutName)
  comboBoxEntrySetTextColumn combo textColumn
  
  return combo
  
addPerfomedHaircutToCustomerAcid theCustomer haircut description state win = 
    do
        theHaircut <- haircut 
        theHaircutDesc <- description
        let newPH = (createPerformedHaircut theHaircut theHaircutDesc)
        let newCtm = addPerformedHaircut theCustomer newPH
        
        -------MODIFICO STATE--------------------
        updatePerformedHaircutToCustomer newCtm state
         -----------------------------------------
         
        addMsg <- messageDialogNew (Just win) [DialogDestroyWithParent] MessageInfo ButtonsOk "Corte agregado"
        dialogRun addMsg
    
        widgetDestroy win
        return () 
 
getSelectedHaircutAcid combo state = do
    index <- comboBoxGetActive combo
    haircuts <- getHaircutsAcid state
    return (searchByIndex index haircuts) 
        



textColumn :: ColumnId Haircut String
textColumn = makeColumnIdString 0 


--------------------------------------------------------------------------

                                         
replaceCustomer :: [Customer] -> Customer -> [Customer]
replaceCustomer (ctm:ctms) customer = if (getCustomerName ctm == getCustomerName customer)
                                          then (customer:ctms)
                                          else ctm:(replaceCustomer ctms customer)
                                          

----------------------------------------------------------------------------------------
